home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODUtil / Sources / FWPoint.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  13.5 KB  |  439 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPoint.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPOINT_H
  13. #include "FWPoint.h"
  14. #endif
  15.  
  16. #ifndef FWRECT_H
  17. #include "FWRect.h"
  18. #endif
  19.  
  20. // ----- Foundation Includes -----
  21.  
  22. #ifndef FWSTREAM_H
  23. #include "FWStream.h"
  24. #endif
  25.  
  26. //========================================================================================
  27. //    RunTime Info
  28. //========================================================================================
  29.  
  30. #if FW_LIB_EXPORT_PRAGMAS
  31. #pragma lib_export on
  32. #endif
  33.  
  34. #ifdef FW_BUILD_MAC
  35. #pragma segment fwodutil
  36. #endif
  37.  
  38. //========================================================================================
  39. //    struct FW_CPoint
  40. //========================================================================================
  41.  
  42. //----------------------------------------------------------------------------------------
  43. //    FW_CPoint::FW_CPoint
  44. //----------------------------------------------------------------------------------------
  45.  
  46. FW_CPoint::FW_CPoint(FW_PlatformPoint plfmPoint) :
  47. #ifdef FW_BUILD_WIN
  48.     x    (FW_IntToFixed(plfmPoint.x)),
  49.     y    (FW_IntToFixed(plfmPoint.y))
  50. #endif
  51. #ifdef FW_BUILD_MAC
  52.     x    (FW_IntToFixed(plfmPoint.h)),
  53.     y    (FW_IntToFixed(plfmPoint.v))
  54. #endif
  55. {
  56. }
  57.  
  58. //----------------------------------------------------------------------------------------
  59. //    FW_CPoint::FW_CPoint
  60. //----------------------------------------------------------------------------------------
  61.  
  62. FW_CPoint::FW_CPoint(const FW_CPoint &point) :
  63.     x(point.x),
  64.     y(point.y)
  65. {
  66. }
  67.  
  68. //----------------------------------------------------------------------------------------
  69. //    FW_CPoint::FW_CPoint
  70. //----------------------------------------------------------------------------------------
  71.  
  72. FW_CPoint::FW_CPoint(const ODPoint &odPoint) :
  73.     x    (FW_ODFixedToFixed(odPoint.x)),
  74.     y    (FW_ODFixedToFixed(odPoint.y))
  75. {
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. //    FW_CPoint::FW_CPoint
  80. //----------------------------------------------------------------------------------------
  81.  
  82. FW_CPoint::FW_CPoint(const FW_CReadableStream& stream)
  83. {
  84.     stream.Read(this, sizeof(FW_CPoint));
  85. }
  86.  
  87. //----------------------------------------------------------------------------------------
  88. //    FW_CPoint::operator=
  89. //----------------------------------------------------------------------------------------
  90.  
  91. FW_CPoint& FW_CPoint::operator=(const FW_CPoint &point)
  92. {
  93.     x = point.x;
  94.     y = point.y;
  95.     return *this;
  96. }
  97.  
  98. //----------------------------------------------------------------------------------------
  99. //    FW_CPoint::operator=
  100. //----------------------------------------------------------------------------------------
  101.  
  102. FW_CPoint& FW_CPoint::operator=(const ODPoint &odPoint)
  103. {
  104.     x = FW_ODFixedToFixed(odPoint.x);
  105.     y = FW_ODFixedToFixed(odPoint.y);
  106.     return *this;
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //    FW_CPoint::operator=
  111. //----------------------------------------------------------------------------------------
  112.  
  113. FW_CPoint& FW_CPoint::operator=(FW_PlatformPoint plfmPoint)
  114. {
  115. #ifdef FW_BUILD_WIN
  116.     x    =    FW_IntToFixed(plfmPoint.x);
  117.     y    =    FW_IntToFixed(plfmPoint.y);
  118. #endif
  119. #ifdef FW_BUILD_MAC
  120.     x    =    FW_IntToFixed(plfmPoint.h);
  121.     y    =    FW_IntToFixed(plfmPoint.v);
  122. #endif
  123.     return *this;
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. //    FW_CPoint::Offset
  128. //----------------------------------------------------------------------------------------
  129.  
  130. void FW_CPoint::Offset(FW_CFixed xx, FW_CFixed yy)
  131. {
  132.     x += xx;
  133.     y += yy;
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. //    FW_CPoint::operator+=
  138. //----------------------------------------------------------------------------------------
  139.  
  140. FW_CPoint& FW_CPoint::operator+=(const FW_CPoint &point)
  141. {
  142.     x += point.x;
  143.     y += point.y;
  144.     return *this;
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. //    FW_CPoint::operator-=
  149. //----------------------------------------------------------------------------------------
  150.  
  151. FW_CPoint& FW_CPoint::operator-=(const FW_CPoint &point)
  152. {
  153.     x -= point.x;
  154.     y -= point.y;
  155.     return *this;
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. //    FW_CPoint::operator+=
  160. //----------------------------------------------------------------------------------------
  161.  
  162. FW_CPoint& FW_CPoint::operator+=(const ODPoint &odPoint)
  163. {
  164.     x += FW_ODFixedToFixed(odPoint.x);
  165.     y += FW_ODFixedToFixed(odPoint.y);
  166.     return *this;
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. //    FW_CPoint::operator-=
  171. //----------------------------------------------------------------------------------------
  172.  
  173. FW_CPoint& FW_CPoint::operator-=(const ODPoint &odPoint)
  174. {
  175.     x -= FW_ODFixedToFixed(odPoint.x);
  176.     y -= FW_ODFixedToFixed(odPoint.y);
  177.     return *this;
  178. }
  179.  
  180. //----------------------------------------------------------------------------------------
  181. //    FW_CPoint::operator+
  182. //----------------------------------------------------------------------------------------
  183.  
  184. FW_CPoint FW_CPoint::operator+(const FW_CPoint &point) const
  185. {
  186.     FW_CPoint aPoint(x + point.x, y + point.y);
  187.     return aPoint;
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. //    FW_CPoint::operator-
  192. //----------------------------------------------------------------------------------------
  193.  
  194. FW_CPoint FW_CPoint::operator-(const FW_CPoint &point) const
  195. {
  196.     FW_CPoint aPoint(x - point.x, y - point.y);
  197.     return aPoint;
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. //    FW_CPoint::operator+
  202. //----------------------------------------------------------------------------------------
  203.  
  204. FW_CPoint FW_CPoint::operator+(const ODPoint &odPoint) const
  205. {
  206.     return FW_CPoint(x + FW_ODFixedToFixed(odPoint.x), y + FW_ODFixedToFixed(odPoint.y));
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. //    FW_CPoint::operator-
  211. //----------------------------------------------------------------------------------------
  212.  
  213. FW_CPoint FW_CPoint::operator-(const ODPoint &odPoint) const
  214. {
  215.     return FW_CPoint(x - FW_ODFixedToFixed(odPoint.x), y - FW_ODFixedToFixed(odPoint.y));
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. //    FW_CPoint::operator-
  220. //----------------------------------------------------------------------------------------
  221.  
  222. FW_CPoint FW_CPoint::operator-() const
  223. {
  224.     return FW_CPoint(-x, -y);
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    FW_CPoint::operator+=
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_CPoint& FW_CPoint::operator+=(FW_SPlatformPoint plfmPoint)
  232. {
  233.     x += FW_IntToFixed(plfmPoint.X());
  234.     y += FW_IntToFixed(plfmPoint.Y());
  235.     return *this;
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    FW_CPoint::operator-=
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_CPoint& FW_CPoint::operator-=(FW_SPlatformPoint plfmPoint)
  243. {
  244.     x -= FW_IntToFixed(plfmPoint.X());
  245.     y -= FW_IntToFixed(plfmPoint.Y());
  246.     return *this;
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. //    FW_CPoint::operator+
  251. //----------------------------------------------------------------------------------------
  252.  
  253. FW_CPoint FW_CPoint::operator+(FW_SPlatformPoint plfmPoint) const
  254. {
  255.     return FW_CPoint(x + FW_IntToFixed(plfmPoint.X()), y + FW_IntToFixed(plfmPoint.Y()));
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. //    FW_CPoint::operator-
  260. //----------------------------------------------------------------------------------------
  261.  
  262. FW_CPoint FW_CPoint::operator-(FW_SPlatformPoint plfmPoint) const
  263. {
  264.     return FW_CPoint(x - FW_IntToFixed(plfmPoint.X()), y - FW_IntToFixed(plfmPoint.Y()));
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. //    FW_CPoint::AsPlatformPoint
  269. //----------------------------------------------------------------------------------------
  270.  
  271. void FW_CPoint::AsPlatformPoint(FW_PlatformPoint& plfmPoint) const
  272. {
  273. #ifdef FW_BUILD_MAC
  274.     plfmPoint.h = x.AsInt();
  275.     plfmPoint.v = y.AsInt();
  276. #endif
  277. #ifdef FW_BUILD_WIN
  278.     plfmPoint.x = x.AsInt();
  279.     plfmPoint.y = y.AsInt();
  280. #endif
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. //    FW_CPoint::operator==
  285. //----------------------------------------------------------------------------------------
  286.  
  287. FW_Boolean FW_CPoint::operator==(const FW_CPoint& pt) const
  288. {
  289.     return x == pt.x && y == pt.y;
  290. }
  291.  
  292. //----------------------------------------------------------------------------------------
  293. //    FW_CPoint::operator!=
  294. //----------------------------------------------------------------------------------------
  295.  
  296. FW_Boolean FW_CPoint::operator!=(const FW_CPoint& pt) const
  297. {
  298.     return x != pt.x || y != pt.y;
  299. }
  300.  
  301. //----------------------------------------------------------------------------------------
  302. //    FW_CPoint::operator==
  303. //----------------------------------------------------------------------------------------
  304.  
  305. FW_Boolean FW_CPoint::operator==(const ODPoint& odPoint) const
  306. {
  307.     return x.AsODFixed() == odPoint.x && y.AsODFixed() == odPoint.y;
  308. }
  309.  
  310. //----------------------------------------------------------------------------------------
  311. //    FW_CPoint::operator!=
  312. //----------------------------------------------------------------------------------------
  313.  
  314. FW_Boolean FW_CPoint::operator!=(const ODPoint& odPoint) const
  315. {
  316.     return x.AsODFixed() != odPoint.x || y.AsODFixed() != odPoint.y;
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. //    FW_CPoint::operator[]
  321. //----------------------------------------------------------------------------------------
  322.  
  323. FW_CFixed& FW_CPoint::operator[](FW_XYSelector selector)
  324. {
  325.     return * (FW_CFixed *) (selector == FW_kVertical ? &y : &x);
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. //    FW_CPoint::operator[]
  330. //----------------------------------------------------------------------------------------
  331.  
  332. FW_CFixed FW_CPoint::operator[](FW_XYSelector selector) const
  333. {
  334.     return * (FW_CFixed *) (selector == FW_kVertical ? &y : &x);
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    FW_CPoint::Map
  339. //----------------------------------------------------------------------------------------
  340.  
  341. void FW_CPoint::Map(const FW_CRect& srcRect, const FW_CRect& destRect)
  342. {
  343.     FW_CFixed xDelta = x - srcRect.left;
  344.     FW_CFixed yDelta = y - srcRect.top;
  345.  
  346.     FW_CFixed yRatio = FW_IntToFixed(1);
  347.  
  348.     if (srcRect.Width())
  349.     {
  350.         FW_CFixed xRatio = destRect.Width() / srcRect.Width();
  351.         xDelta *= xRatio;
  352.     }
  353.  
  354.     if (srcRect.Height())
  355.     {
  356.         FW_CFixed yRatio = destRect.Height() / srcRect.Height();
  357.         yDelta *= yRatio;
  358.     }
  359.  
  360.     x = destRect.left + xDelta;
  361.     y = destRect.top + yDelta;
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. //    FW_CPoint::Transform
  366. //----------------------------------------------------------------------------------------
  367.  
  368. void FW_CPoint::Transform(Environment* ev, ODTransform* transform)
  369. {
  370. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  371.     * ((ODPoint *) this) =
  372. #endif
  373.     transform->TransformPoint(ev, (ODPoint*) this);
  374. }
  375.  
  376. //----------------------------------------------------------------------------------------
  377. //    FW_CPoint::InverseTransform
  378. //----------------------------------------------------------------------------------------
  379.  
  380. void FW_CPoint::InverseTransform(Environment* ev, ODTransform* transform)
  381. {
  382. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  383.     * ((ODPoint *) this) =
  384. #endif
  385.     transform->InvertPoint(ev, (ODPoint*) this);
  386. }
  387.  
  388. //----------------------------------------------------------------------------------------
  389. //    FW_CPoint::TransformCopy
  390. //----------------------------------------------------------------------------------------
  391.  
  392. FW_CPoint FW_CPoint::TransformCopy(Environment* ev, ODTransform* transform) const
  393. {
  394.     FW_CPoint point(*this);
  395.     
  396. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  397.     * ((ODPoint *) & point) = 
  398. #endif
  399.     transform->TransformPoint(ev, (ODPoint*)&point);
  400.  
  401.     return point;
  402. }
  403.  
  404. //----------------------------------------------------------------------------------------
  405. //    FW_CPoint::InverseTransformCopy
  406. //----------------------------------------------------------------------------------------
  407.  
  408. FW_CPoint FW_CPoint::InverseTransformCopy(Environment* ev, ODTransform* transform) const
  409. {
  410.     FW_CPoint point(*this);
  411.     
  412. #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
  413.     * ((ODPoint *) & point) = 
  414. #endif
  415.     transform->InvertPoint(ev, (ODPoint*)&point);
  416.  
  417.     return point;
  418. }
  419.  
  420. //----------------------------------------------------------------------------------------
  421. //    operator<<
  422. //----------------------------------------------------------------------------------------
  423.  
  424. FW_FUNC_ATTR const FW_CWritableStream& operator<<(const FW_CWritableStream& stream, const FW_CPoint& point)
  425. {
  426.     stream.Write(&point, sizeof(FW_CPoint));
  427.     return stream;
  428. }
  429.  
  430. //----------------------------------------------------------------------------------------
  431. //    operator>>
  432. //----------------------------------------------------------------------------------------
  433.  
  434. FW_FUNC_ATTR const FW_CReadableStream& operator>>(const FW_CReadableStream& stream, FW_CPoint& point)
  435. {
  436.     stream.Read(&point, sizeof(FW_CPoint));
  437.     return stream;
  438. }
  439.